<?php
class Articles_Model {
	public $con;
	public function __construct(){
		$this->con =  mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
		if(mysqli_connect_error())
		{
			echo "Failed to connect to MySQL". mysqli_connect_error();exit();
		}	
	}
	
	public function getListsArt() {
		$query = "SELECT * FROM articles ";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function getAllArtInACat($cat) {
		$query = "SELECT ar.id,ar.title,ar.content,ar.image,ar.idCat,ar.alias,ca.nameCat FROM articles AS ar, categories AS ca
				WHERE ar.idCat=ca.id
				AND ca.nameCat='$cat'";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function getAArticle($id) {
		$query = "SELECT ar.id,ar.title,ar.image,ar.content,ar.idCat,ar.alias,ca.nameCat FROM articles AS ar, categories AS ca 
		WHERE ar.id='$id'
		AND ar.idCat=ca.id";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function getTittle($id) {
		$query = "SELECT title FROM articles WHERE id='$id' ";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function getImage($id) {
		$query = "SELECT image FROM articles WHERE id='$id' ";
		$result = mysqli_query($this->con,$query);
		return $result;
	}

	public function addArt($title,$content,$img,$idcat,$alias) {
		$query = "INSERT INTO articles(title,image,content,idCat,alias) VALUES ('$title','$img','$content','$idcat','$alias')";
		$result = mysqli_query($this->con,$query);
	}
	
	function deleteArticles($id){
		$query = "DELETE FROM articles WHERE id = '$id'";
		mysqli_query($this->con,$query);
    }
}
?>